from requests import Session
from time import time
from hashlib import sha1
from base64 import b64encode


def show(movies: list):
    for movie in movies:
        print(f"{movie['name']} - {movie['alias']}")
        print("  Score:", movie["score"])
        print("  Tags:", *movie["categories"])
        print("  Regions:", *movie["regions"])
        print(f'  Duration: {movie["minute"]} min')
        print("  Release date:", movie["published_at"])
        print("  Link:", f"https://spa2.scrape.center/detail/{movie['id']}")
        print()


def generate_token(*args) -> str:
    t = str(int(time()))
    e = len(args)
    r = [None, None]
    for i in range(e):
        r[i] = args[i]
    r.append(t)
    o = sha1(",".join(str(item) for item in r).encode()).hexdigest()
    c = b64encode(f"{o},{t}".encode("utf-8")).decode()
    print('token:' + c)
    return c


if __name__ == "__main__":
    start = time()
    x = Session()
    LIMIT = 10  # 每次最多获取10条数据
    URL = "https://spa2.scrape.center/api/movie/"
    r = x.get(
        URL, params={"token": generate_token("/api/movie", 0)}
    )
    show(r.json()["results"])
    end = time()
    input(f"Time used: {end - start} s.")
